package cn.itlaobing.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

public class BaseController implements AController{

	private static final String VIEW_ROOT="/WEB-INF/views";
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		

	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
	}
	
	//请求转发
	protected void toView(String viewName,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		if(!StringUtils.startsWith(viewName, "/")){
			viewName="/"+viewName;
		}
		request.getRequestDispatcher(VIEW_ROOT+viewName).forward(request, response);
	}
	//重新定向
	protected void redirectTo(String url,HttpServletRequest request,HttpServletResponse response) throws IOException {
		if(!StringUtils.startsWith(url, "/")){
			url="/"+url;
		}
		String location=request.getContextPath()+url;
		response.sendRedirect(location);
	}
	
}
